﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class NavTreeGridData 
    {
        public string id { get; set; }
        public bool loaded { get; set; }
        public string text { get; set; }
        public bool expanded { get; set; }
        public List<NavTreeGridData> children { get; set; }
        public string NavType { get; set; }
        public string NavUrl { get; set; }
        public string OpenType { get; set; }
        public string parentid { get; set; }
        public int? Sort { get; set; }
        public string Status { get; set; }
    }

    public class TreeData
    { 
        public bool expanded {get;set;}
        public string parentid { get; set; }
        public string text { get; set; }
        public string id {get;set;}
        public bool leaf { get; set; }
        public List<TreeData> children { get; set; }
    }

    public class NavBarModel : BaseModel
    {
        public string ID { get; set; }
        public string NavName { get; set; }
        public string NavType { get; set; }
        public string NavUrl { get; set; }
        public string OpenType { get; set; }
        public string Status { get; set; }
        public Nullable<int> Sort { get; set; }
        public string ParentID { get; set; }

        public string ParentNavList { get; set; }

        public NavBarModel()
        {
            ParentNavList = "[]";
        }

        private void ConvertTreeGridData(NavTreeGridData parent, List<NavTreeGridData> data)
        {
            parent.children = data.Where(x => x.parentid == parent.id).ToList();
            foreach (var item in parent.children)
            {
                ConvertTreeGridData(item, data);
            }
        }

        public List<NavTreeGridData> GetNavList()
        {
            var rp = new SystemRepository();
            var data = rp.GetNavBarList();
            List<NavTreeGridData> list = data.Select(x => new NavTreeGridData
            {
                id = x.ID,
                loaded = true,
                NavType = Common.SysEnum.ConvertValue<Common.SysEnum.NavType>(x.NavType),
                NavUrl = string.IsNullOrEmpty(x.NavUrl) ? "" : x.NavUrl,
                OpenType = Common.SysEnum.ConvertValue<Common.SysEnum.OpenType>(x.OpenType),
                parentid = x.ParentID,
                Sort = x.Sort,
                Status = Common.SysEnum.ConvertValue<Common.SysEnum.NavStatus>(x.Status),
                text = x.NavName,
                expanded = true
            }).ToList();
            var parent = list.Where(x => x.parentid == Guid.Empty.ToString()).ToList();
            foreach (var item in parent)
            {
                ConvertTreeGridData(item, list);
            }
            return parent;
        }

        public void Init()
        {
            var rp = new SystemRepository();
            var navList = rp.GetNavBarList();
            var data = navList.Where(x => x.ID != ID).Select(x => new TreeData { expanded = true, parentid = x.ParentID, text = x.NavName, id = x.ID }).ToList();
            var classList = data.Where(x => x.parentid == Guid.Empty.ToString()).ToList();
            foreach (var item in classList)
            {
                GetTreeData(item, data);
            }
            classList.Insert(0, new TreeData { expanded = true, id = Guid.Empty.ToString(), text = "===请选择===", leaf = true });
            ParentNavList = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
        }

        public void LoadData(string id)
        {
            var rp = new SystemRepository();
            var navBar = rp.GetNavBarByID(id);
            ID = navBar.ID;
            NavName = navBar.NavName;
            NavType = navBar.NavType;
            NavUrl = navBar.NavUrl;
            OpenType = navBar.OpenType;
            ParentID = navBar.ParentID;
            Sort = navBar.Sort;
            Status = navBar.Status;
        }

        public void Add()
        {
            TP_NavBar navBar = new TP_NavBar
            {
                NavName = NavName,
                NavType = NavType,
                NavUrl = NavType == "10" ? "/" : NavUrl,
                OpenType = OpenType,
                ParentID = ParentID,
                Sort = Sort,
                Status = Status
            };
            var rp = new SystemRepository();
            rp.AddNavBar(navBar);
        }

        public void Update()
        {
            TP_NavBar navBar = new TP_NavBar
            {
                ID = ID,
                NavName = NavName,
                NavType = NavType,
                NavUrl = NavType == "10" ? "/" : NavUrl,
                OpenType = OpenType,
                ParentID = ParentID,
                Sort = Sort,
                Status = Status
            };
            var rp = new SystemRepository();
            rp.UpdateNavBar(navBar);
        }

        public bool HasLeafBavBar(string id)
        {
            var rp = new SystemRepository();
            return rp.HasLeafBavBar(id);
        }

        public void Delete(string id)
        {
            var rp = new SystemRepository();
            rp.DeleteBavBar(id);
        }
    }
}